﻿#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

template<class K, class V>
struct AVLTreeNode
{
	pair<K, V> _kv;
	AVLTreeNode<K, V>* _left;  //左子树
	AVLTreeNode<K, V>* _right; //右子树
	AVLTreeNode<K, V>* _parent;//当前节点的父节点
	int _bf;//平衡因子

	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		,_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{ }
};

template<class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
				return false;
		}
		cur = new Node(kv);
		if (parent->_kv.first > kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;  //链接_parent
		
		//更新平衡因子
		while (parent) //parent为空时证明更新到根节点了
		{
			if (cur == parent->_left) //链接在左边
			{
				parent->_bf--;
			}
			else  //链接在右边
			{
				parent->_bf++;
			}

			if (parent->_bf == 0)
			{
				break;
			}
			else if(parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//旋转逻辑
				if (parent->_bf == -2 && cur->_bf == -1) //右旋
					rotateR(parent);
				else if (parent->_bf == 2 && cur->_bf == 1)//左旋
					rotateL(parent);
				else if (parent->_bf == -2 && cur->_bf == 1)//左右双旋
					rotateLR(parent);
				else if (parent->_bf == 2 && cur->_bf == -1)//右左双旋
					rotateRL(parent);
				else
					assert(false);
				break;
			}
			else
			{
				assert(false);
			}
		}
		return true;
	}

	void rotateR(Node* parent) //右旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		
		parent->_left = subLR;
		if(subLR) //防止对空指针解引用
			subLR->_parent = parent;

		Node* pParent = parent->_parent; //先记录旋转前parent的父节点

		subL->_right = parent;
		parent->_parent = subL;
		if (pParent == nullptr) //旋转前parent为根节点
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else //旋转前parent不为根节点
		{
			subL->_parent = pParent;
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}
		}

		//更新平衡因子
		subL->_bf = 0;
		parent->_bf = 0;
	}

	void rotateL(Node* parent) //左旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL; 
		if(subRL)
			subRL->_parent = parent;

		Node* pParent = parent->_parent; //记录parent的_parent
		subR->_left = parent;
		parent->_parent = subR;

		if (pParent == nullptr)//更新前的parent是整棵树根节点
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			subR->_parent = pParent;

			if (pParent->_left == parent)
				pParent->_left = subR;
			else
				pParent->_right = subR;
		}

		subR->_bf = 0; //更新平衡因子
		parent->_bf = 0;
	}

	void rotateLR(Node* parent) //左右双旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		rotateL(parent->_left); //先左旋
		rotateR(parent); //再右旋

		if (bf == -1)
		{
			subL->_bf = subLR->_bf = 0;
			parent->_bf = 1;
		}
		else if (bf == 1)
		{
			subLR->_bf = parent->_bf = 0;
			subL->_bf = -1;
		}
		else if (bf == 0)
		{
			subLR->_bf = parent->_bf = subL->_bf = 0;
		}
		else
			assert(false);
	}

	void rotateRL(Node* parent) //右左双旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		rotateR(parent->_right); //先右旋
		rotateL(parent);  //再左旋

		if (bf == 1)
		{
			subR->_bf = 0;
			subRL->_bf = 0;
			parent->_bf = -1;
		}
		else if (bf == -1)
		{
			subRL->_bf = 0;
			parent->_bf = 0;
			subR->_bf = 1;
		}
		else if (bf == 0)
		{
			subRL->_bf = 0;
			parent->_bf = 0;
			subR->_bf = 0;
		}
		else
			assert(false);
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

	void Inoder() //中序遍历
	{
		_Inorder(_root);
		cout << endl;
	}

	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}

	int Height()
	{
		return _Height(_root);
	}

	int Size()
	{
		return _Size(_root);
	}

private:
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	
	bool _IsBalanceTree(Node* root)
	{
		// 空树也是AVL树
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因⼦：即pRoot左右⼦树的⾼度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		// 如果计算出的平衡因⼦与pRoot的平衡因⼦不相等，或者
		// pRoot平衡因⼦的绝对值超过1，则⼀定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		}
		if (root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}
		// pRoot的左和右如果都是AVL树，则该树⼀定是AVL树
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}

	void _Inorder(const Node* root) 
	{
		if (root == nullptr)
			return;
		_Inorder(root->_left);
		cout << root->_kv.first  << ":" << root->_kv.second << ' ';
		_Inorder(root->_right);
	}

	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;
		return _Size(root->_left) + _Size(root->_right) + 1;
	}

	Node* _root = nullptr;
};

